<?php

ini_set('include_path', '../');

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/model_exception.php");
require_once("lib/exception/parameters_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/autor/dao/autor_dao_factory.php");
require_once("model/autor/to/autor_to.php");
require_once("model/autor_message/to/autor_message_to.php");

require_once("model/autorfacade/to/basic_autor_to.php");
require_once("model/autorfacade/to/statics_autor_to.php");


require_once("model/autorfacade/delegate/autor_facade_delegate_factory.php");



class Autorfacade_test {
	
	private $dao;
	private $autor;
	private $autor2;
	
	public function before() {
		try {
			$this->dao = Autor_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			throw $e;		
		}
	}

	public function run() {
		try {
		
			$autor_facade = Autor_facade_delegate_factory::singleton()->get_delegate();
			
			//crea un autor
			$autor_to = new Autor_to('xxxxxxxxxxxx','pp','email');						
			$this->autor = $autor_facade->register_autor($autor_to);
			echo 'Create autor: ' . $this->autor->to_string() . '</br>';
			
			//login de usuario correto con pasword encrypted
			$basic_autor = $autor_facade->login($this->autor->get_login(), $this->autor->get_password(), true);
			echo 'Login autor with correct cryp pass: ' . $basic_autor->to_string() . '</br>';
			
			//login de usuario correto con pasword no encrypted
			$basic_autor = $autor_facade->login($this->autor->get_login(), $autor_to->get_password(), false);
			echo 'Login autor with correct single pass: ' . $basic_autor->to_string() . '</br>';
			
			//login de usuario incorrecto con pasword que no existe
			try {
				$basic_autor = $autor_facade->login($this->autor->get_login(), $autor_to->get_password(), true);
				throw new Exception('login user with incorrect password');
			}
			catch (Parameters_exception $e) {
				echo 'Correct error to login with bad password</br>';
			}
			
			//change password for existent user
			$autor_facade->change_password($this->autor->get_login(), $autor_to->get_password(), 'peces');
			$basic_autor = $autor_facade->login($this->autor->get_login(), 'peces', false);
			echo 'Correct changed password to user: ' . $basic_autor->to_string() . '</br>';
			
			//change password for inexistent user
			try {
				$autor_facade->change_password('pepe', $autor_to->get_password(), 'peces');
				throw new Exception('password changed by user inexistent');
			}
			catch (Model_exception $e) {
				echo 'Correct error to change password by inexistent user</br>';
			}
			
			//change password for incorrect password
			try {
				$autor_facade->change_password($this->autor->get_login(), 'pacote', 'peces');
				throw new Exception('password changed with pass incorrect');
			}
			catch (Parameters_exception $e) {
				echo 'Correct error to change password with incorrect password</br>';
			}
						
			//get user info
			$autor = $autor_facade->get_user_info($this->autor->get_login());
			echo 'Get user info: ' . $autor->to_string() . '</br>';
			
			//change user dates
			$this->autor->set_email('calabaza');
			$autor_facade->update_user($this->autor);
			$autor = $autor_facade->get_user_info($this->autor->get_login());
			echo 'update user: ' . $autor->to_string() . '</br>';
			
			//get statics by user
			$statics = $autor_facade->get_statics_user($this->autor->get_login());
			echo 'Statics for user: ' . $statics->to_string() . '</br>';		

			//Auxiliar operations
			$aux_autor = new Autor_to('yyyyyyyyyyyy','pp2','email2');						
			$this->autor2 = $autor_facade->register_autor($aux_autor);
			
			//Send a sms to inexistent user
			try {
				$date = date("Y-m-d H:i:s");
				$message = new Autor_message_to(0, $this->autor->get_login(), 'pp', 
														'title', 'content', $date, 'new');
				$message = $autor_facade->send_message($message);
				throw new Exception('send message to inexistent user');
			}
			catch (Parameters_exception $e) {
				echo 'Correct error sending message to inexistent user</br>';
			}
			
			//Send a sms to existent user
			$date = date("Y-m-d H:i:s");
			$message = new Autor_message_to(0, $this->autor->get_login(), $this->autor2->get_login(), 
														'title', 'content', $date, 'new');
			$message = $autor_facade->send_message($message);
			echo "Send message: " . $message->to_string() . "</br>";
			
			//Get all mail inbox information
			$all_inbox = $autor_facade->get_all_mail_inbox($this->autor2->get_login(), 0, 10);
			echo 'Get all mail inbox information: </br>' . $all_inbox->to_string() . '</br>';
			
			//Get all mail send information
			$all_inbox = $autor_facade->get_all_mail_send($this->autor->get_login(), 0, 10);
			echo 'Get all mail send information: </br>' . $all_inbox->to_string() . '</br>';
			
			//get inexistent message
			try {
				$autor_facade->get_message(99999);
				throw new Exception('Found inexistent message');
			}
			catch (Model_exception $e) {
				echo 'Correct error to get message inexistent</br>';
			}
			
			//get full message
			$message = $autor_facade->get_message($message->get_number());
			echo 'Get full message: ' . $message->to_string() . '</br>';
			
			//change message state to new
			$autor_facade->change_message_to_new($message->get_number());
			$all_inbox = $autor_facade->get_all_mail_inbox($this->autor2->get_login(), 0, 10);
			echo 'Change message state to new: ' . $all_inbox->to_string() . '</br>';

			//change message state to old
			$autor_facade->change_message_to_old($message->get_number());
			$all_inbox = $autor_facade->get_all_mail_inbox($this->autor2->get_login(), 0, 10);
			echo 'Change message state to old: ' . $all_inbox->to_string() . '</br>';
			
			//delete message
			$autor_facade->delete_message($message->get_number());
			try {
				$all_inbox = $autor_facade->get_all_mail_inbox($this->autor2->get_login(), 0, 10);
			}
			catch (Model_exception $e) {
				echo 'Delete message:</br>' . $all_inbox->to_string() . '</br>';
			}
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Model_exception $e) {
			throw $e;
		}
		catch (Parameters_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function after() {
		try {
			$this->dao->remove($this->autor->get_login());
			$this->dao->remove($this->autor2->get_login());
		}
		catch (Exception $e) {
			throw $e;
		}
		
	}
}

$test = new Autorfacade_test();

try {
	$test->before();
	$test->run();
	$test->after();
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>